import request from '@/utils/request'
import { Message } from 'element-ui'

/**
 * post请求
 * @param {*} url 
 * @param {*} data 
 * @returns 
 */
export function post(url, data, params, finallyCallback, hasShowSucceedMessage = true) {
  return new Promise((resolve, reject) => {
    return request({
      url: url,
      method: 'post',
      data: data,
      params: params
    }).then(response => {
      if (!response) {
        return;
      }
      if (response.hasSuccessful) {
        if (hasShowSucceedMessage) {
          Message({
            message: response.message,
            type: 'success',
            duration: 5 * 1000
          })
        }
        resolve(response.data)
      } else {
        reject(response.message)
      }
    }).catch(error => {
      Message({
        message: error,
        type: 'error',
        duration: 5 * 1000
      })
      reject(error)
    }).finally(() => {
      if (finallyCallback) {
        finallyCallback()
      }
    })
  });
}

/**
 * get 请求
 * @param {*} data 
 * @returns 
 */
export function get(url, data) {
  return new Promise((resolve, reject) => {
    return request({
      url: url,
      method: 'get',
      params: data
    }).then(response => {
      if (!response) {
        return;
      }
      if (response.hasSuccessful) {
        resolve(response.data)
      } else {
        reject(response.message)
      }
    }).catch(error => {
      Message({
        message: error,
        type: 'error',
        duration: 5 * 1000
      })
    })
  });
}